<?php
/*
 * Generate captcha
 * 1. Create a captcha image
 * 2. Store created captcha in the captcha table
 * 3. Return the captcha
 */ 
function generateCaptcha($ip_address = ''){
	$CI =& get_instance();
	$CI->load->model('captcha_m');
	$CI->load->plugin('captcha_pi');
	$vals = array(
				'img_path'	 => './application/assets/captcha/',
				'img_url'	 => BASE_URL."/application/assets/captcha/"
			);
	$cap = create_captcha($vals);
	$cap['ip_address'] = $ip_address;
	$CI->captcha_m->addCaptcha($cap);
	return $cap;
}

/*
 * Validate captcha
 * 1. Delete expired captchas
 * 2. Validate captcha
 * @param $id Dùng trong trường hợp nhiều captcha trên 1 màn hình, mỗi captcha cần có id khác nhau
 * @return: true if captcha is valid
 * 			false if captcha is invalid
 */
function validateCaptcha($word, $id=''){
	/*$CI =& get_instance();
	$CI->load->model('captcha_m');
	
	//Delete expired captchas
	$CI->captcha_m->deleteExpiredCaptchas();
	
	//Validate captcha
	if ($CI->captcha_m->getCaptcha($word, $ip_address)){
		return true;
	} else {
		return false;
	}*/
	require('application/captcha/php-captcha.inc.php');
	if (PhpCaptcha::Validate($word, true, $id)) {
		return true;
	} else {
		return false;
	}
}
/**
 * validate captcha for module payment
 * @param unknown_type $word
 * @param unknown_type $key_session
 * @author:tuanvd
 */
function validateCaptchaPayment($word,$key_session){
	require_once('application/captcha/php_captcha_extends.php');
	if (PHP_Captcha_Exts::Validate($word,true,$key_session)) {
		return true;
	} else {
		return false;
	}
}
/**
 * Get captcha error message according to language
 * @return unknown_type
 */
function  getCaptchaErrorMessage(){
   if(strcmp($_SESSION['payment_lang_code'],"en")==0){
   		$error_message ="Safety code is not correct!";
   }else{
   		$error_message ="Mã an toàn nhập không chính xác!";
   }
   return $error_message;
}

?>
